package com.chenchao.net;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/24 18:32
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.net.Test03.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * Socket套节字 模拟客户端
 */
public class Test03 {
    public static void main(String[] args) throws IOException {
        /*客户端输入*/
        /*1.创建一个流套接字并将其连接到指定主机的指定端口号上*/
        Socket socket = new Socket("127.0.0.1",8080);
        /*2.获取socket的输出流*/
        OutputStream outputStream = socket.getOutputStream();
        /*3.写入数据*/
        outputStream.write("Hello,server!".getBytes());
        socket.shutdownOutput();
        /*关闭写入*/

        /*客户端输出*/
        /*1.接收服务器端写回的数据流*/
        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        /*2.读出数据*/
        char[] chars = new char[1024];
        int len ;
        while ((len = isr.read(chars))!=-1){
            System.out.println(new String(chars,0,len));
        }
        /*3.释放资源*/
        socket.close();
    }
}
